﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Runda.CMS.Dal;
using System.Data;
using Runda.CMS.Common.Entity;
using Runda.CMS.Common.Utility;
using Runda.CMS.Common.Log;
using log4net;

namespace Runda.CMS.Bll
{    
    public class MyDesktopBll:CacheBase
    {
        private ILog log = log4net.LogManager.GetLogger("myLogger");

        public MyDesktopBll()
        {
            //_cacheName = "";
        }

        public DataTable getPriWebPart(Guid UserID) {
            DataTable dt = new DataTable();
            SysWebpartsDal dal = new SysWebpartsDal();
            return dal.getPrivateWebPart(UserID);            
        }

        public DataTable getPublicWebPart() {
            DataTable dt = new DataTable();
            SysWebpartsDal dal = new SysWebpartsDal();
            return dal.getPublicWebPart();          
        }

        public DataTable getPublicDesktopWebPart() { 
            return new SysDesktopWebPartsDal().getPubliDesktopWebParts();
        }

        public DataTable getPrivateDesktopWebPart(Guid UserID) {
            return new SysDesktopWebPartsDal().getPrivateDesktopWebParts(UserID);
        }

        public void persistMyDeskTop(SysDesktopEntity desktopEntity)
        {
            SysDesktopBll deskBll = new SysDesktopBll();
            SysDesktopWebPartsBll desktopwebPartsBll = new SysDesktopWebPartsBll();

            using (System.Transactions.TransactionScope trans = new System.Transactions.TransactionScope())
            {
                deskBll.Insert(desktopEntity);
                foreach (SysDesktopWebPartsEntity entity in desktopEntity.SysDesktopWebPartsEntityList)
                {
                    desktopwebPartsBll.Insert(entity);
                }
                trans.Complete();
            }
            log.Info(new LogContent(LogType.info, "MyDesktopBll", desktopEntity.Owner.ToString(), "", "建立个人工作台"));
        }

        public void mergeMyDesktop(SysDesktopEntity desktopEntity)
        {
            SysDesktopBll deskBll = new SysDesktopBll();
            SysDesktopWebPartsBll desktopwebPartsBll = new SysDesktopWebPartsBll();
            using (System.Transactions.TransactionScope trans = new System.Transactions.TransactionScope())
            {
                deskBll.Update(desktopEntity);
                List<SqlDbParameter> parms = new List<SqlDbParameter>();
                SqlDbParameter parm = new SqlDbParameter();
                parm.ColumnName = "DesktopID";
                parm.ColumnType = DbType.Guid;
                parm.ParameterName = "@DesktopID";
                parm.ParameterValue = desktopEntity.DesktopID;
                parm.QualificationType = SqlDbParameter.QualificationSymbol.Equal;
                parms.Add(parm);
                desktopwebPartsBll.Delete<SysDesktopWebPartsEntity>(parms);

                foreach (SysDesktopWebPartsEntity entity in desktopEntity.SysDesktopWebPartsEntityList)
                {
                    desktopwebPartsBll.Insert(entity);
                }
                trans.Complete();
            }
            log.Info(new LogContent(LogType.info, "MyDesktopBll", desktopEntity.Owner.ToString(), "", "修改个人工作台"));
        }
    }
}
